package com.arcvideo.acsplayer.widget;

import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;

public class BrightnessView extends FrameLayout {
	private Context mContext = null;
	private Activity mActivity = null;
	private ImageButton mBrightness = null;
	private VerticalSeekBar mBrightnessSeekBar = null;
	private TextView mBrightnessPrecentTextView = null;
	private int mBrightnessCurrent = 0;
	private int mBrightnessMax = 0;

	/**
	 * 进度条事件
	 */
	private OnSeekBarChangeListener mSeekBarChangeListener = new OnSeekBarChangeListener() {

		// 触发操作，拖动
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			setBrightness(progress);
		}

		// 表示进度条刚开始拖动，开始拖动时候触发的操作
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		// 停止拖动时候
		public void onStopTrackingTouch(SeekBar seekBar) {
			setBrightness(seekBar.getProgress());
		}

	};

	/**
	 * 按键点击事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.brightness :
					break;

				default :
					break;
			}
		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public BrightnessView(Context context) {
		super(context);
		initUi();
		initData();
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public BrightnessView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
		initUi();
		initData();
	}

	/**
	 * 初始化UI
	 */
	private void initUi() {
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_brightness, this);
		mBrightness = (ImageButton) findViewById(R.id.brightness);
		mBrightnessSeekBar = (VerticalSeekBar) findViewById(R.id.brightness_seekbar);
		// mBrightnessSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener);
		mBrightness.setOnClickListener(mOnClickListener);
		mBrightnessPrecentTextView = (TextView) findViewById(R.id.brightness_precent);
	}

	/**
	 * 初始化數據
	 */
	public void initData() {
		initBrightness();
		mBrightnessSeekBar.setMax(mBrightnessMax);
		mBrightnessSeekBar.setProgress(mBrightnessCurrent);
		mBrightnessPrecentTextView.setText("" + mBrightnessCurrent * 100 / 255);
	}

	/**
	 * 设置activity
	 *
	 * @param activity
	 */
	public void setActivity(Activity activity) {
		mActivity = activity;
	}

	/**
	 * 显示
	 */
	public void show() {
		initData();
		setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏
	 */
	public void hide() {
		setVisibility(View.GONE);
	}

	/**
	 * 设置亮度
	 *
	 * @param brightness
	 */
	private void setBrightness(int brightness) {
		if (mActivity == null)
			return;
		// 不让屏幕全暗
		if (brightness <= 1) {
			brightness = 1;
		}
		// 设置当前activity的屏幕亮度
		WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
		// 0到1,调整亮度暗到全亮
		lp.screenBrightness = Float.valueOf(brightness / 255f);
		mActivity.getWindow().setAttributes(lp);

		// 保存为系统亮度方法1
		Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);

		mBrightnessSeekBar.setProgress(brightness);

		// 更改亮度文本显示
		mBrightnessPrecentTextView.setText("" + brightness * 100 / 255);
	}

	/**
	 * 设置亮度
	 *
	 * @param percent
	 */
	public void setBrightness(float percent) {
		if (mActivity == null)
			return;

		// 不让屏幕全暗
		if (percent <= 0.01) {
			percent = (float) 0.01;
		}

		int value = (int) (percent * mBrightnessMax) + mBrightnessCurrent;
		if (value > mBrightnessMax)
			value = mBrightnessMax;
		else if (value < 0)
			value = 0;

		// 设置当前activity的屏幕亮度
		WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
		// 0到1,调整亮度暗到全亮
		lp.screenBrightness = Float.valueOf(value / 255f);
		mActivity.getWindow().setAttributes(lp);

		// 保存为系统亮度方法1
		Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);

		mBrightnessSeekBar.setProgress(value);

		// 更改亮度文本显示
		mBrightnessPrecentTextView.setText("" + value * 100 / 255);
	}

	/**
	 * 增加亮度
	 */
	public void increase() {
		mBrightnessCurrent += 15;
		if (mBrightnessCurrent >= mBrightnessMax)
			mBrightnessCurrent = mBrightnessMax;

		setBrightness(mBrightnessCurrent);
	}

	/**
	 * 降低亮度
	 */
	public void decrease() {
		if (mBrightnessCurrent >= 15)
			mBrightnessCurrent -= 15;
		else
			mBrightnessCurrent = 1;
		setBrightness(mBrightnessCurrent);
	}

	/**
	 * 初始化亮度
	 */
	private void initBrightness() {
		// 关闭系统的亮度自动调节
		stopAutoBrightness(mContext);

		// 获取当前亮度,获取失败则返回255
		mBrightnessCurrent = (int) (Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255));
		mBrightnessMax = 255;
	}

	/**
	 * 停止自动亮度调节
	 */
	public static void stopAutoBrightness(Context context) {
		try {
			Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
					Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 开启亮度自动调节
	 *
	 */
	public static void startAutoBrightness(Context context) {
		try {
			Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
					Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}